package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwgiT;
import cn.e4j.modular.crm.entity.GwgjT;
import cn.e4j.modular.crm.entity.GwgkT;
import cn.e4j.modular.crm.mapper.Gwgi005Mapper;
import cn.e4j.modular.crm.service.Gwgi005Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
public class Gwgi005ServiceImpl extends ServiceImpl<Gwgi005Mapper, GwgiT> implements Gwgi005Service {
    @Override
    public List<Dto> getHeadList(Page page, Dto inDto) {
        return InvokeUtil.invokeBeanMethod("gwgi005Mapper,getHeadList", page, inDto);
    }

    @Override
    public List<Dto> getGwgdData(Dto inDto) {
        return this.baseMapper.getGwgdData(inDto);
    }

    @Override
    @Transactional
    public void add(GwgiT gwgiT, List<GwgjT> gwgjTList, List<GwgkT> gwgkTList) {
        gwgiT.insert();
        gwgjTList.forEach(Model::insert);
        gwgkTList.forEach(Model::insert);
    }

    @Override
    @Transactional
    public void delete(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String ENT = shiroUser.getDBC();
        String gwgiID = inDto.getString("GWGIID");

        UpdateWrapper<GwgiT> gwgiTUpdateWrapper = new UpdateWrapper<>();
        gwgiTUpdateWrapper.eq("GWGIENT", ENT);
        gwgiTUpdateWrapper.eq("GWGIID", gwgiID);
        new GwgiT().delete(gwgiTUpdateWrapper);

        UpdateWrapper<GwgjT> gwgjTUpdateWrapper = new UpdateWrapper<>();
        gwgjTUpdateWrapper.eq("GWGJENT", ENT);
        gwgjTUpdateWrapper.eq("GWGJID", gwgiID);
        new GwgjT().delete(gwgjTUpdateWrapper);

        UpdateWrapper<GwgkT> gwgkTUpdateWrapper = new UpdateWrapper<>();
        gwgkTUpdateWrapper.eq("GWGKENT", ENT);
        gwgkTUpdateWrapper.eq("GWGKID", gwgiID);
        new GwgkT().delete(gwgkTUpdateWrapper);
    }


    @Override
    @Transactional
    public void deleteBatch(List<GwgiT> gwgiTList) {
        gwgiTList.forEach(gwgiT -> {
            Dto paramDto = Dtos.newDto();
            paramDto.put("ENT", ShiroKit.getUser().getDBC());
            paramDto.put("GWGIID", gwgiT.getGWGIID());
            this.delete(paramDto);
        });
    }

    @Override
    public void changeStatusBatch(List<GwgiT> gwgiTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String ENT = shiroUser.getDBC();
        gwgiTList.forEach(gwgiT -> {
            if (gwgiT.getGWGIACTI().equals("T")) { //提交时更新提交时间
                gwgiT.setGWGI020(LocalDate.now());
            }
            if (gwgiT.getGWGIACTI().equals("N")) { //取消提交时清空提交时间
                gwgiT.setGWGI020(null);
            }
            if (gwgiT.getGWGIACTI().equals("Y")) { //审核时更新审核时间以及审核人员
                gwgiT.setGWGI021(shiroUser.getID());
                gwgiT.setGWGI022(LocalDate.now());
            }
            UpdateWrapper<GwgiT> gwgiTUpdateWrapper = new UpdateWrapper<>();
            gwgiTUpdateWrapper.eq("GWGIENT", ENT);
            gwgiTUpdateWrapper.eq("GWGIID", gwgiT.getGWGIID());
            gwgiT.update(gwgiTUpdateWrapper);
        });
    }

    @Override
    public List<Dto> getBookmarkData(Page page,Dto inDto) {
        return InvokeUtil.invokeBeanMethod("gwgi005Mapper,getBookmarkData", page, inDto);
    }

    @Override
    @Transactional
    public void edit(GwgiT gwgiT, List<GwgjT> gwgjTList, List<GwgkT> gwgkTList) {
        UpdateWrapper<GwgiT> gwgiTUpdateWrapper = new UpdateWrapper<>();
        gwgiTUpdateWrapper.eq("GWGIENT", gwgiT.getGWGIENT());
        gwgiTUpdateWrapper.eq("GWGIID", gwgiT.getGWGIID());
        gwgiT.update(gwgiTUpdateWrapper);

        if (gwgjTList.size() > 0){
            GwgjT gwgjT = gwgjTList.get(0);
            UpdateWrapper<GwgjT> gwgjTUpdateWrapper = new UpdateWrapper<>();
            gwgjTUpdateWrapper.eq("GWGJENT", gwgjT.getGWGJENT());
            gwgjTUpdateWrapper.eq("GWGJID", gwgjT.getGWGJID());
            gwgjT.delete(gwgjTUpdateWrapper);
            gwgjTList.forEach(Model::insert);
        }

        if(gwgkTList.size() > 0) {
            GwgkT gwgkT = gwgkTList.get(0);
            UpdateWrapper<GwgkT> gwgkTUpdateWrapper = new UpdateWrapper<>();
            gwgkTUpdateWrapper.eq("GWGKENT", gwgkT.getGWGKENT());
            gwgkTUpdateWrapper.eq("GWGKID", gwgkT.getGWGKID());
            gwgkT.delete(gwgkTUpdateWrapper);
            gwgkTList.forEach(Model::insert);
        }
    }

    @Override
    public boolean existedTheParams(GwgiT gwgiT) {
        QueryWrapper<GwgiT> gwgiTQueryWrapper = new QueryWrapper<>();
        gwgiTQueryWrapper.eq("GWGIENT", gwgiT.getGWGIENT());
        gwgiTQueryWrapper.eq("GWGIID", gwgiT.getGWGIID());
        return this.baseMapper.selectList(gwgiTQueryWrapper).size() > 0;
    }

    @Override
    public List<Dto> getGwgjData(Dto inDto) {
        return this.baseMapper.getGwgjData(inDto);
    }

    @Override
    public List<GwgkT> getGwgkData(Dto inDto) {
        QueryWrapper<GwgkT> gwgkTQueryWrapper = new QueryWrapper<>();
        gwgkTQueryWrapper.eq("GWGKENT", inDto.getString("ENT"));
        gwgkTQueryWrapper.eq("GWGKID", inDto.getString("GWGIID"));
        return new GwgkT().selectList(gwgkTQueryWrapper);
    }
}
